package hi

import (
	"log"
	"micro/bff/internal/grpc"
	"micro/pkg/logger"
	pb "micro/proto/hipb"
	"net/http"
	"time"

	"github.com/uptrace/opentelemetry-go-extra/otelzap"

	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

type HelloResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    Hello  `json:"data"`
}

type Hello struct {
	Time time.Time
}

// Hello godoc
// @Tags Hello
// @Summary get Hello information
// @Accept text/plain
// @Produce json
// @Success 200 {object} HelloResponse
// @Failure default {object} HTTPError
// @Router /gethello [get]
func (c *HiController) GetHello(ctx *gin.Context) {
	//res, err := hiSrv.HiClient.SayHello(ctx, &pb.HelloRequest{Name: "world"})
	//md := metadata.Pairs(
	//	"timestamp", time.Now().Format(time.StampNano),
	//	//"client-id", "from bff id 1233213434324322",
	//	//"user-id", "211",
	//)
	chainCtx := grpc.TraceCtx(ctx)
	res, err := grpc.HiClient.SayHello(chainCtx, &pb.HelloRequest{Name: "world"})
	logger.L.Info("get hello", zap.Any("res", res))
	//添加tag
	otelLogger := otelzap.New(logger.L)
	otelLogger.Ctx(chainCtx).Error(">>>>>>>get hello", zap.Any("res", res))
	//添加日志
	grpc.Span.AddEvent("2222222	 -------------->")
	defer grpc.Span.End()
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("----------------->Greeting: %s", res.Message)
	ctx.JSON(http.StatusOK, HelloResponse{Code: http.StatusOK, Message: "hello world", Data: Hello{Time: time.Now()}})
	//ctx.JSON(200, gin.H{"code": 0, "message": "hello world", "data": model.Hello{Time: time.Now()}})
}
